package org.springblade.pm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.pm.entity.Contract;
import org.springblade.pm.service.IContractService;
import org.springblade.pm.vo.ContractVo;
import org.springblade.pm.vo.PageQuery;
import org.springblade.pm.wrappper.ContractWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 合同
 */
@RestController
@RequestMapping("contract")
@AllArgsConstructor
public class ContractController {

	private IContractService contractService;

	@GetMapping("/getContractList")
	@ApiOperation(value = "获取合同列表")
	public R getContractList(Contract contract, PageQuery pageQuery) {
		QueryWrapper<Contract> wrapper = new QueryWrapper<>();
		wrapper.eq("project_id", contract.getProjectId());
		if (contract.getContractCode() != null && !"".equals(contract.getContractCode())) {
			wrapper.like("contract_code", contract.getContractCode());
		}
		if (contract.getContractStatus() != null && !"".equals(contract.getContractName())) {
			wrapper.eq("contract_status", contract.getContractStatus());
		}
		IPage<Contract> page = contractService.page(Condition.getPage(pageQuery), wrapper);
		List<ContractVo> contractVos = ContractWrapper.build().entityVoList(page.getRecords());
		IPage<ContractVo> contractVoIPage = ContractWrapper.build().pageVO(page);
		contractVoIPage.setRecords(contractVos);
		return R.data(contractVoIPage);
	}

	@PostMapping("/submit")
	@ApiOperation(value = "新增合同")
	public R submit(Contract contract, BladeUser bladeUser) {
		if (contract.getId()==null||"".equals(contract.getId())){
			contract.setCreateUser(bladeUser.getUserId());
			contract.setCreateTime(new Date());
		}
		return R.status(contractService.saveOrUpdate(contract));
	}

	@GetMapping("/contractDetails")
	@ApiOperation(value = "查询合同详情")
	public R contractDetails(Integer id) {
		return R.data(contractService.getById(id));
	}

	@GetMapping("/deleteContract")
	@ApiOperation(value = "删除合同")
	public R deleteContract(Integer id) {
		return R.status(contractService.removeById(id));
	}
}
